#include "ui_session_message.h"

namespace
{
std::string typeToString(UiSessionMessage::Type type)
{
    switch (type)
    {
    case UiSessionMessage::Type::Success:
        return "success";
    case UiSessionMessage::Type::Warning:
        return "warning";
    case UiSessionMessage::Type::Alert:
        return "alert";
    }
    return "success";
}
} // namespace

UiSessionMessage::UiSessionMessage(
    const crow::json::wvalue& caption,
    const crow::json::wvalue& text,
    Type type
)
    : m_caption(caption)
    , m_text(text)
    , m_type(type)
{
}

Common::ContextValue UiSessionMessage::toContextValue() const
{
    Common::ContextValue result {
        { "type", typeToString(m_type) },
        { "caption", m_caption },
        { "text", m_text },
    };
    return result;
}

void UiSessionMessageList::push(const UiSessionMessage& message)
{
    std::unique_lock locker(m_lock);
    m_data.push_back(message);
}

Common::ContextList UiSessionMessageList::toContextList()
{
    std::unique_lock locker(m_lock);
    Common::ContextList result;
    for (const auto& item : m_data)
        result << item.toContextValue();
    m_data.clear();
    return result;
}

Common::ContextValue UiSessionMessageList::toContextValue()
{
    return toContextList().data();
}
